/*
 * Copyright (C) 2016 Jared Rummler Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the
 * License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or
 * agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.kongzue.baseokhttpv3.slice;

import java.util.NoSuchElementException;

import ohos.agp.components.Attr;
import ohos.agp.components.AttrSet;
import ohos.agp.utils.Color;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 *
 * @ClassName:      TypedAttrUtils
 * @Description:    描述
 * @CreateDate:     2021/6/8 20:37
 * @UpdateUser:     更新者
 * @UpdateDate:     2021/6/8 20:37
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
public final class TypedAttrUtils {

    static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00201, "zhk");

    /**
     * getIntColor
     *
     * @param attrs    attrs
     * @param attrName attrName
     * @param defValue defValue
     * @return int
     */
    public static int getIntColor(AttrSet attrs, String attrName, int defValue) {
        Attr attr = attrNoSuchElement(attrs, attrName);
        if (attr == null) {
            return defValue;
        } else {
            return attr.getColorValue().getValue();
        }
    }

    /**
     * getColor
     *
     * @param attrs    attrs
     * @param attrName attrName
     * @param defValue defValue
     * @return Color
     */
    public static Color getColor(AttrSet attrs, String attrName, Color defValue) {
        Attr attr = attrNoSuchElement(attrs, attrName);
        if (attr == null) {
            return defValue;
        } else {
            return attr.getColorValue();
        }
    }

    /**
     * getInteger
     *
     * @param attrs    attrs
     * @param attrName attrName
     * @param defValue defValue
     * @return int
     */
    public static int getInteger(AttrSet attrs, String attrName, int defValue) {
        Attr attr = attrNoSuchElement(attrs, attrName);
        if (attr == null) {
            return defValue;
        } else {
            return attr.getIntegerValue();
        }
    }

    /**
     * getFloat
     *
     * @param attrs    attrs
     * @param attrName attrName
     * @param defValue defValue
     * @return float
     */
    public static float getFloat(AttrSet attrs, String attrName, float defValue) {
        Attr attr = attrNoSuchElement(attrs, attrName);
        if (attr == null) {
            return defValue;
        } else {
            return attr.getFloatValue();
        }
    }

    /**
     * getLong
     *
     * @param attrs    attrs
     * @param attrName attrName
     * @param defValue defValue
     * @return long
     */
    public static long getLong(AttrSet attrs, String attrName, long defValue) {
        Attr attr = attrNoSuchElement(attrs, attrName);
        if (attr == null) {
            return defValue;
        } else {
            return attr.getLongValue();
        }
    }

    /**
     * getBoolean
     *
     * @param attrs    attrs
     * @param attrName attrName
     * @param defValue defValue
     * @return boolean
     */
    public static boolean getBoolean(AttrSet attrs, String attrName, boolean defValue) {
        Attr attr = attrNoSuchElement(attrs, attrName);
        if (attr == null) {
            return defValue;
        } else {
            return attr.getBoolValue();
        }
    }

    /**
     * getString
     *
     * @param attrs    attrs
     * @param attrName attrName
     * @param defValue defValue
     * @return String
     */
    public static String getString(AttrSet attrs, String attrName, String defValue) {
        Attr attr = attrNoSuchElement(attrs, attrName);
        if (attr == null) {
            return defValue;
        } else {
            return attr.getStringValue();
        }
    }

    /**
     * getDimensionPixelSize
     *
     * @param attrs    attrs
     * @param attrName attrName
     * @param defValue defValue
     * @return int
     */
    public static int getDimensionPixelSize(AttrSet attrs, String attrName, int defValue) {
        Attr attr = attrNoSuchElement(attrs, attrName);
        if (attr == null) {
            return defValue;
        } else {
            return attr.getDimensionValue();
        }
    }

    /**
     * getLayoutDimension
     *
     * @param attrs    attrs
     * @param attrName attrName
     * @param defValue defValue
     * @return int
     */
    public static int getLayoutDimension(AttrSet attrs, String attrName, int defValue) {
        Attr attr = attrNoSuchElement(attrs, attrName);
        if (attr == null) {
            return defValue;
        } else {
            HiLog.info(label, "attr.getDimensionValue() = " + attr.getDimensionValue());
            return attr.getDimensionValue();
        }
    }

    private static Attr attrNoSuchElement(AttrSet attrs, String attrName) {
        Attr attr = null;
        try {
            attr = attrs.getAttr(attrName).get();
        } catch (NoSuchElementException e) {
            HiLog.info(label, "Exception = " + e.toString());
        }
        return attr;
    }
}
